package com.example.demo.service.impl;

import com.example.demo.dto.RoleConfigDTO;
import com.example.demo.entity.RoleConfig;
import com.example.demo.mapper.RoleConfigMapper;
import com.example.demo.request.CreateRoleConfigRequest;
import com.example.demo.service.RoleConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleConfigServiceImpl implements RoleConfigService {

    @Autowired
    private RoleConfigMapper roleConfigMapper;

    @Override
    public List<RoleConfigDTO> getRoleConfigs(int page, int size) {
        List<RoleConfig> roleConfigs = roleConfigMapper.selectAll();
        return roleConfigs.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public long countRoleConfigs() {
        return roleConfigMapper.count();
    }

    @Override
    public RoleConfigDTO getRoleConfigById(Integer id) {
        RoleConfig roleConfig = roleConfigMapper.selectById(id);
        return convertToDTO(roleConfig);
    }

    @Override
    public RoleConfig createRoleConfig(CreateRoleConfigRequest request) {
        RoleConfig roleConfig = new RoleConfig();
        roleConfig.setRoleName(request.getRoleName());
        roleConfig.setRoleDescription(request.getRoleDescription());
        roleConfig.setConfigName(request.getConfigName());
        roleConfig.setIsActive(request.getIsActive());
        roleConfig.setFieldConfig(request.getFieldConfig());
        roleConfig.setDepartmentAssignments(request.getDepartmentAssignments());
        roleConfig.setFieldPriorities(request.getFieldPriorities());
        roleConfigMapper.insert(roleConfig);
        return roleConfig;
    }

    @Override
    public void updateRoleConfig(Integer id, CreateRoleConfigRequest request) {
        RoleConfig roleConfig = roleConfigMapper.selectById(id);
        roleConfig.setRoleName(request.getRoleName());
        roleConfig.setRoleDescription(request.getRoleDescription());
        roleConfig.setConfigName(request.getConfigName());
        roleConfig.setIsActive(request.getIsActive());
        roleConfig.setFieldConfig(request.getFieldConfig());
        roleConfig.setDepartmentAssignments(request.getDepartmentAssignments());
        roleConfig.setFieldPriorities(request.getFieldPriorities());
        roleConfigMapper.updateById(roleConfig);
    }

    @Override
    public void deleteRoleConfig(Integer id) {
        roleConfigMapper.deleteById(id);
    }
    
    @Override
    public List<RoleConfigDTO> searchRoleConfigsByRoleName(String roleName) {
        List<RoleConfig> roleConfigs = roleConfigMapper.selectByRoleNameLike(roleName);
        return roleConfigs.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private RoleConfigDTO convertToDTO(RoleConfig roleConfig) {
        return RoleConfigDTO.fromEntity(roleConfig);
    }
}